/*
 * @# AlbumRedis.java 2011-12-26 下午04:40:27
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.px.album.redis;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;

import com.ianswer.px.album.entity.Album;
import com.ianswer.px.common.redis.RedisSupport;

/**
 * 相册在redis中的处理.
 * 
 * @author alvin hwang
 */
public class AlbumRedis extends RedisSupport {

	private static final String ALBUM_KEY = "album:";
	private static final String USERID = "userid";
	private static final String ADDTIME = "addtime";
	private static final String PHOTOS = "photos";
	private static final String FAVORS = "favors";
	private static final String LIKES = "likes";
	private static final String VISITS = "visits";
	private static final String TRANSFERRED = "transferred";
	private static final String COMMENTS_TOTAL = "commentstotal";
	private static final String COMMENTS = "comments";

	public static void init(Album album) {
		Map<String, String> albumCache = new HashMap<String, String>();
		albumCache.put(ADDTIME, album.getCreatedAt().toString());
		albumCache.put(USERID, album.getUserId().toString());
		albumCache.put(PHOTOS, "0");
		albumCache.put(FAVORS, "0");
		albumCache.put(LIKES, "0");
		albumCache.put(VISITS, "0");
		albumCache.put(TRANSFERRED, "0");
		albumCache.put(COMMENTS_TOTAL, "0");
		albumCache.put(COMMENTS, "0");

		redis().hmset(key(album.getId()), albumCache);
	}

	public static boolean delete(Long albumId) {
		return redis().del(key(albumId)) > 0;
	}
	
	public static Album get(Long albumId) {
		Map<String, String> albumCache = redis().hgetAll(key(albumId));
		if (MapUtils.isEmpty(albumCache)) {
			return null;
		}
		Album album = new Album();
		album.setId(albumId);
		album.setCreatedAt(getLong(albumCache, ADDTIME));
		album.setPhotos(getInt(albumCache, PHOTOS));
		album.setFavors(getInt(albumCache, FAVORS));
		album.setLikes(getInt(albumCache, LIKES));
		album.setVisits(getInt(albumCache, VISITS));
		album.setTransferred(getInt(albumCache, TRANSFERRED));
		album.setCommentsTotal(getInt(albumCache, COMMENTS_TOTAL));
		album.setComments(getInt(albumCache, COMMENTS));
		
		return album;
	}

	public static int photos(Long albumId) {
		return getInt(key(albumId), PHOTOS);
	}
	
	public static int likes(Long albumId) {
		return getInt(key(albumId), LIKES);
	}
	
	public static int favors(Long albumId) {
		return getInt(key(albumId), FAVORS);
	}
	
	public static int transferred(Long albumId) {
		return getInt(key(albumId), TRANSFERRED);
	}
	
	public static int commentsTotal(Long albumId) {
		return getInt(key(albumId), COMMENTS_TOTAL);
	}

	public static void incrPhotosBy(Long albumId, int photos) {
		redis().hincrBy(key(albumId), PHOTOS, photos);
	}

	public static void decrPhotosBy(Long albumId, int photos) {
		redis().hincrBy(key(albumId), PHOTOS, -photos);
	}

	public static void incrFavors(Long albumId) {
		redis().hincrBy(key(albumId), FAVORS, 1);
	}

	public static void decrFavors(Long albumId) {
		redis().hincrBy(key(albumId), FAVORS, -1);
	}

	public static void incrLikes(Long albumId) {
		redis().hincrBy(key(albumId), LIKES, 1);
	}

	public static void decrLikes(Long albumId) {
		redis().hincrBy(key(albumId), LIKES, -1);
	}

	public static void incrVisits(Long albumId) {
		redis().hincrBy(key(albumId), VISITS, 1);
	}

	public static void decrVisits(Long albumId) {
		redis().hincrBy(key(albumId), VISITS, -1);
	}

	public static void incrTransferred(Long albumId) {
		redis().hincrBy(key(albumId), TRANSFERRED, 1);
	}

	public static void decrTransferred(Long albumId) {
		redis().hincrBy(key(albumId), TRANSFERRED, -1);
	}

	public static void incrCommentsTotal(Long albumId) {
		redis().hincrBy(key(albumId), COMMENTS_TOTAL, 1);
	}

	public static void incrComments(Long albumId) {
		redis().hincrBy(key(albumId), COMMENTS, 1);
	}

	public static void decrComments(Long albumId) {
		redis().hincrBy(key(albumId), COMMENTS, -1);
	}

	private static String key(Long albumId) {
		return ALBUM_KEY + albumId;
	}
}
